//
//  ModuleAPI.swift
//  
//
//  Created by 孙长坦 on 2022/4/6.
//

import Foundation

import Foundation
import UIKit
import Moya
import LibBase
import LibNetwork

enum ModuleAPI {
    case pageValue(moduleType: String, moduleId: String?,
                   pageType: String, pageId: String?)
}
    
extension ModuleAPI: APITargetType {
    var path: String {
        switch self {
        case .pageValue:
            return "/environment/pageValue"
        }
    }
    
    var task: Task {
        switch self {
        case .pageValue(let moduleType, let moduleId,
                        let pageType, let pageId):
            
            var parameters: [String: Any] = ["moduleType": moduleType,
                                             "pageType": pageType]
            
            if let moduleId = moduleId {
                parameters["moduleId"] = moduleId
            }
            
            if let pageId = pageId {
                parameters["pageId"] = pageId
            }
            
            return .requestParameters(parameters: parameters,
                                      encoding: URLEncoding.queryString)
        }
    }
    
    var method: Moya.Method {
        return .get
    }
    
    var sampleData: Data {
        switch self {
        case .pageValue(_, let moduleId,
                        _, let pageId):
            guard let jsonPath = ModuleManager.shared.dataSource?.jsonPath else {
                return Data()
            }
            
            var parameters: [String] = []
            
            if let moduleId = moduleId {
                parameters.append(moduleId)
            }
            
            if let pageId = pageId {
                parameters.append(pageId)
            }
            
            return Bundle.main.ex
                .data(forResource: "\(jsonPath)/\(parameters.joined(separator: "_"))",
                      withExtension: "json") ?? Data()
        }
    }
}
